package com.linghang.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linghang.demo.domain.FootPic;
import com.linghang.demo.domain.FootPicExample;
import com.linghang.demo.mapper.FootPicMapper;
import com.linghang.demo.req.FootPicQueryReq;
import com.linghang.demo.req.FootPicSaveReq;
import com.linghang.demo.resp.FootPicQueryResp;
import com.linghang.demo.resp.PageResp;
import com.linghang.demo.util.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FootPicService {
    private int id=10;
    @Resource
    private FootPicMapper foot_picMapper;
    private static final Logger LOG = LoggerFactory.getLogger(FootPicService.class);

    public List<FootPic> list(){
        return foot_picMapper.selectByExample(null);
    }

    public PageResp<FootPicQueryResp> list(FootPicQueryReq req) {
        FootPicExample foot_picExample = new FootPicExample();
        FootPicExample.Criteria criteria = foot_picExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getId())) {
            criteria.andIdEqualTo(  req.getId() );
        }

        PageHelper.startPage(req.getPage(), req.getSize());
        List<FootPic> foot_picList = foot_picMapper.selectByExample(foot_picExample);

        PageInfo<FootPic> pageInfo = new PageInfo<>(foot_picList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        // List<FootPicResp> respList = new ArrayList<>();
        // for (FootPic foot_pic : foot_picList) {
        //     // FootPicResp foot_picResp = new FootPicResp();
        //     // BeanUtils.copyProperties(foot_pic, foot_picResp);
        //     // 对象复制
        //     FootPicResp foot_picResp = CopyUtil.copy(foot_pic, FootPicResp.class);
        //
        //     respList.add(foot_picResp);
        // }

        // 列表复制
        List<FootPicQueryResp> list = CopyUtil.copyList(foot_picList, FootPicQueryResp.class);

        PageResp<FootPicQueryResp> pageResp = new PageResp();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);

        return pageResp;
    }

    /**
     * 保存
     */
    public void save(FootPicSaveReq req) {
        FootPic foot_pic = CopyUtil.copy(req, FootPic.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            // 新增
            foot_pic.setId(id++);
            foot_picMapper.insert(foot_pic);
        } else {
            // 更新
            foot_picMapper.updateByPrimaryKey(foot_pic);
        }
    }

    
    public void  delete(int id){
        foot_picMapper.deleteByPrimaryKey(id);
    }
}
